import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文
plt.rcParams['axes.unicode_minus'] = False    # 正常显示负号

# 实验三：复数的几何应用 (简化版)
print("实验三：复数的几何应用")
print("="*40)

# 椭圆上的点
def ellipse_points(a, c):
    modulus_a, modulus_c = abs(a), abs(c)
    if modulus_a > modulus_c:
        return None, None
    semi_major = modulus_c
    semi_minor = np.sqrt(modulus_c**2 - modulus_a**2)
    return semi_major, semi_minor

print("1. 椭圆上的点:")
a_val, c_val = 1 + 1j, 2 + 1j
print(f"  a = {a_val}, |a| = {abs(a_val):.4f}")
print(f"  c = {c_val}, |c| = {abs(c_val):.4f}")
print(f"  |a| <= |c|: {abs(a_val) <= abs(c_val)}")

if abs(a_val) <= abs(c_val):
    max_mod, min_mod = ellipse_points(a_val, c_val)
    print(f"  |z|的最大值: {max_mod:.4f}")
    print(f"  |z|的最小值: {min_mod:.4f}")

# 对称点
def reflection(a):
    return np.imag(a) + 1j*np.real(a)

print("\n2. 对称点:")
a_point = 2 + 3j
reflected = reflection(a_point)
print(f"  点 a = {a_point}")
print(f"  关于 y=x 的对称点: {reflected}")

# 正方形顶点
def find_square(a, b):
    vector = b - a
    perp = 1j * vector
    c, d = a + perp, b + perp
    return c, d

print("\n3. 正方形顶点:")
vertex_a, vertex_b = 0 + 0j, 2 + 0j
vertex_c, vertex_d = find_square(vertex_a, vertex_b)
print(f"  已知顶点: a = {vertex_a}, b = {vertex_b}")
print(f"  另两个顶点: c = {vertex_c}, d = {vertex_d}")

# 三角形外接圆
def circumcircle(a1, a2, a3):
    a1_conj, a2_conj, a3_conj = np.conj(a1), np.conj(a2), np.conj(a3)
    numerator = (abs(a1)**2 * (a2-a3) + abs(a2)**2 * (a3-a1) + abs(a3)**2 * (a1-a2))
    denominator = (a1*(a2_conj-a3_conj) + a2*(a3_conj-a1_conj) + a3*(a1_conj-a2_conj))
    if abs(denominator) < 1e-10:
        return None, None
    center = numerator / denominator
    radius = abs(a1 - center)
    return center, radius

print("\n4. 三角形外接圆:")
triangle_a1, triangle_a2, triangle_a3 = 0 + 0j, 3 + 0j, 1.5 + 2j
center, radius = circumcircle(triangle_a1, triangle_a2, triangle_a3)
print(f"  外接圆圆心: {center:.4f}")
print(f"  外接圆半径: {radius:.4f}")

# 平行四边形性质
def verify_parallelogram(a, b, c, d):
    diag1_mid = (a + c) / 2
    diag2_mid = (b + d) / 2
    return abs(diag1_mid - diag2_mid) < 1e-10

print("\n5. 平行四边形性质:")
para_a, para_b, para_c, para_d = 0 + 0j, 3 + 0j, 4 + 2j, 1 + 2j
diagonals_bisect = verify_parallelogram(para_a, para_b, para_c, para_d)
print(f"  对角线相互平分: {diagonals_bisect}")

# 可视化几何性质
print("\n6. 可视化几何性质:")
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(10, 8))

# 正方形
square_x = [0, 2, 2, 0, 0]
square_y = [0, 0, 2, 2, 0]
ax1.plot(square_x, square_y, 'b-', linewidth=2)
ax1.scatter([0, 2, 2, 0], [0, 0, 2, 2], color='red', s=30)
ax1.set_xlim(-0.5, 2.5)
ax1.set_ylim(-0.5, 2.5)
ax1.set_title('正方形')
ax1.grid(True)

# 三角形及其外接圆
triangle_x = [0, 3, 1.5, 0]
triangle_y = [0, 0, 2, 0]
ax2.plot(triangle_x, triangle_y, 'g-', linewidth=2)
ax2.scatter([0, 3, 1.5], [0, 0, 2], color='red', s=30)
center_val = 1.5 + 0.5j
radius_val = abs(0 - center_val)
circle = plt.Circle((np.real(center_val), np.imag(center_val)), radius_val, fill=False, color='purple', linestyle='--')
ax2.add_patch(circle)
ax2.scatter([np.real(center_val)], [np.imag(center_val)], color='black', s=20)
ax2.set_xlim(-0.5, 3.5)
ax2.set_ylim(-0.5, 2.5)
ax2.set_title('三角形和外接圆')
ax2.grid(True)

# 平行四边形
parallelogram_x = [0, 3, 4, 1, 0]
parallelogram_y = [0, 0, 2, 2, 0]
ax3.plot(parallelogram_x, parallelogram_y, 'm-', linewidth=2)
ax3.scatter([0, 3, 4, 1], [0, 0, 2, 2], color='red', s=30)
ax3.plot([0, 4], [0, 2], 'k--', linewidth=1)
ax3.plot([3, 1], [0, 2], 'k--', linewidth=1)
ax3.set_xlim(-0.5, 4.5)
ax3.set_ylim(-0.5, 2.5)
ax3.set_title('平行四边形')
ax3.grid(True)

# 圆和弦
circle_points = np.exp(1j * np.linspace(0, 2*np.pi, 100))
ax4.plot(np.real(circle_points), np.imag(circle_points), 'k-', linewidth=2)
for i in range(-2, 3):
    y_offset = i * 0.3
    if abs(y_offset) < 1:
        x_range = np.sqrt(1 - y_offset**2)
        ax4.plot([-x_range, x_range], [y_offset, y_offset], 'b-', linewidth=1)
        ax4.plot([0], [y_offset], 'ro', markersize=2)
ax4.set_xlim(-1.2, 1.2)
ax4.set_ylim(-1.2, 1.2)
ax4.set_title('圆的平行弦')
ax4.grid(True)

plt.tight_layout()
plt.show()